<?php

class ProductController extends ZendX_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }
        
	public function getnewestproductAction($limit = 10)
	{
		$svc = new App_Service_Jsonp();
		$res = array();
	
		$client = $this->couchClient();
		$suppliers = $client->reduce(false)->limit($limit)->descending(true)->getView("services", "getSupplier");
		foreach ($suppliers->rows as $s) {
			
			$products = $client->reduce(false)
								->startkey(array($s->value->_id,array()))
								->endkey(array($s->value->_id))
								->limit(1)
								->descending(true)
								->getView("services", "getProductBySupplier");
			if (count($products->rows) > 0) {
				$p = $products->rows[0]->value;
				$res[] = array("_id" => $p->_id, "name" => $p->name);
			}
		}

		echo $_GET["jsonpcallback"].'('.$svc->JSON(array('result' => $res)).')';
		exit();
	}


}

